//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.ObjectModel;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class BubbleSeries : Series, IDataPointOwner
    {
        private bool? bubble3D;
        private NumericDataSource bubbleSize;
        private DataLabels dlbls;
        private readonly DataPoints dPt = new DataPoints();
        private ErrorBars _errorsBars1;
        private bool invertIfNegative;

        private readonly ObservableCollection<TrendLine> trendLines = new ObservableCollection<TrendLine>();

        private DataSource xValues;

        private NumericDataSource yValues;
        private ErrorBars _errorBars2;


        public bool InvertIfNegative
        {
            get { return invertIfNegative; }
            set { invertIfNegative = value; notifyPropertyChanged("InvertIfNegative"); }
        }

        public ObservableCollection<TrendLine> TrendLines
        {
            get { return trendLines; }
        }

        public DataSource XValues
        {
            get { return xValues; }
            set { xValues = value; notifyPropertyChanged("XValues");}
        }

        public NumericDataSource YValues
        {
            get { return yValues; }
            set { yValues = value; notifyPropertyChanged("YValues"); }
        }

        public NumericDataSource BubbleSize
        {
            get { return bubbleSize; }
            set { bubbleSize = value; notifyPropertyChanged("BubbleSize"); }
        }

        public bool? Bubble3D
        {
            get { return bubble3D; }
            set { bubble3D = value; notifyPropertyChanged("Bubble3D"); }
        }


        public DataPoints DataPoints
        {
            get { return dPt; }
            
        }


        public ErrorBars ErrorBars1
        {
            get { return _errorsBars1; }
            set { _errorsBars1 = value; notifyPropertyChanged("ErrorBars1"); }
        }
        public ErrorBars ErrorBars2
        {
            get { return _errorBars2; }
            set { _errorBars2 = value; notifyPropertyChanged("ErrorBars2"); }
        }

        public DataLabels DataLabels
        {
            get { return dlbls; }
            set { dlbls = value; notifyPropertyChanged("DataLabels"); }
        }

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("ser"))
            {
                ReadXmlBase(c);
                invertIfNegative = c.ReadOptionalElementBooleanAttribute("invertIfNegative", "val", false, true);
                DataPoints.Clear();
                while (c.ReaderLocalName == "dPt")
                {
                    DataPoints.Add(c.ReadElement<DataPoint>());
                }
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                while (c.ReaderLocalName == TrendLine.xmlName)
                {
                    TrendLines.Add(c.ReadElement<TrendLine>());
                }
                ErrorBars1 = c.ReadOptionalElement<ErrorBars>(ErrorBars.xmlName);
                ErrorBars2 = c.ReadOptionalElement<ErrorBars>(ErrorBars.xmlName);
                XValues = DataSource.ReadOptionalDataSource("xVal", c);
                YValues = NumericDataSource.ReadNumericDataSource("yVal", c);
                BubbleSize = NumericDataSource.ReadOptionalNumericDataSource("bubbleSize", c);
                Bubble3D = c.ReadOptionalElementBooleanAttribute("bubble3D");
                c.WarnIf("ext");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("ser"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElementBooleanAttribute("invertIfNegative", InvertIfNegative);
                c.WarnIf("dPt");
                c.WriteOptionalElement(DataLabels);
                foreach (TrendLine l in TrendLines)
                    c.WriteElement(l);
                c.WriteOptionalElement(ErrorBars1);
                c.WriteOptionalElement(ErrorBars2);
                WriteOptionalNumericDataSource("xVal", XValues, c);
                WriteOptionalNumericDataSource("yVal", YValues, c);
                //\\c.WriteOptionalElementAs(YValues, "yVal");
                WriteOptionalNumericDataSource("bubbleSize", BubbleSize, c);
                c.WriteOptionalElementBooleanAttribute("bubble3D", Bubble3D);
                c.WarnIf("ext");
            }
        }
    }
}